class ScoreOutOfBoundsException(Exception):
    pass


class Person:
    def __init__(self, id, name):
        self.id = id
        self.name = name

    def __str__(self):
        return f"ID: {self.id}, 姓名: {self.name}"


class Student(Person):
    def __init__(self, id, name, java_score, math_score, english_score):
        super().__init__(id, name)

        # 验证分数范围
        if not (0 <= java_score <= 100 and 0 <= math_score <= 100 and 0 <= english_score <= 100):
            raise ScoreOutOfBoundsException("分数必须在0-100之间")

        self._java_score = java_score
        self._math_score = math_score
        self._english_score = english_score

    @property
    def java_score(self):
        return self._java_score

    @java_score.setter
    def java_score(self, value):
        if not 0 <= value <= 100:
            raise ScoreOutOfBoundsException("Java分数必须在0-100之间")
        self._java_score = value

    @property
    def math_score(self):
        return self._math_score

    @math_score.setter
    def math_score(self, value):
        if not 0 <= value <= 100:
            raise ScoreOutOfBoundsException("数学分数必须在0-100之间")
        self._math_score = value

    @property
    def english_score(self):
        return self._english_score

    @english_score.setter
    def english_score(self, value):
        if not 0 <= value <= 100:
            raise ScoreOutOfBoundsException("英语分数必须在0-100之间")
        self._english_score = value

    def get_total_score(self):
        return self._java_score + self._math_score + self._english_score

    def get_average_score(self):
        return self.get_total_score() / 3

    def __str__(self):
        return (f"{super().__str__()}, Java成绩: {self._java_score:.1f}, "
                f"数学成绩: {self._math_score:.1f}, 英语成绩: {self._english_score:.1f}, "
                f"总分: {self.get_total_score():.1f}, 平均分: {self.get_average_score():.1f}")


class ScoreManager:
    def __init__(self):
        self.students = []

    def show_menu(self):
        while True:
            print("\n===== 学生成绩管理系统 =====")
            print("1. 添加学生")
            print("2. 显示所有学生")
            print("3. 查找学生")
            print("4. 修改学生信息")
            print("5. 删除学生")
            print("6. 统计成绩")
            print("7. 退出系统")

            try:
                choice = input("请选择操作(1-7): ").strip()

                if choice == '1':
                    self.add_student()
                elif choice == '2':
                    self.display_all_students()
                elif choice == '3':
                    self.search_student()
                elif choice == '4':
                    self.update_student()
                elif choice == '5':
                    self.delete_student()
                elif choice == '6':
                    self.show_statistics()
                elif choice == '7':
                    print("感谢使用学生成绩管理系统！")
                    break
                else:
                    print("无效的选择，请重新输入！")
            except ValueError as e:
                print(f"输入错误: {e}")
            except ScoreOutOfBoundsException as e:
                print(f"分数错误: {e}")
            except Exception as e:
                print(f"发生错误: {e}")

    def add_student(self):
        print("\n----- 添加学生 -----")
        id = input("请输入学号: ").strip()

        # 检查学号是否已存在
        for student in self.students:
            if student.id == id:
                print("该学号已存在！")
                return

        name = input("请输入姓名: ").strip()

        try:
            java_score = float(input("请输入Java成绩: "))
            math_score = float(input("请输入数学成绩: "))
            english_score = float(input("请输入英语成绩: "))

            student = Student(id, name, java_score, math_score, english_score)
            self.students.append(student)
            print("学生添加成功！")

        except ValueError:
            print("输入错误，请输入正确的数字格式！")

    def display_all_students(self):
        print("\n----- 所有学生信息 -----")
        if not self.students:
            print("没有学生信息！")
            return

        for i, student in enumerate(self.students, 1):
            print(f"{i}. {student}")

    def search_student(self):
        print("\n----- 查找学生 -----")
        keyword = input("请输入要查找的学生学号或姓名: ").strip()

        found = False
        for student in self.students:
            if student.id == keyword or student.name == keyword:
                print(f"找到学生: {student}")
                found = True

        if not found:
            print("未找到匹配的学生！")

    def update_student(self):
        print("\n----- 修改学生信息 -----")
        id = input("请输入要修改的学生学号: ").strip()

        for student in self.students:
            if student.id == id:
                print(f"找到学生: {student}")

                try:
                    java_score = float(input(f"请输入新的Java成绩(当前: {student.java_score}): "))
                    math_score = float(input(f"请输入新的数学成绩(当前: {student.math_score}): "))
                    english_score = float(input(f"请输入新的英语成绩(当前: {student.english_score}): "))

                    student.java_score = java_score
                    student.math_score = math_score
                    student.english_score = english_score

                    print("学生信息修改成功！")
                    return

                except ValueError:
                    print("输入错误，请输入正确的数字格式！")
                    return

        print(f"未找到学号为 {id} 的学生！")

    def delete_student(self):
        print("\n----- 删除学生 -----")
        id = input("请输入要删除的学生学号: ").strip()

        for i, student in enumerate(self.students):
            if student.id == id:
                print(f"将要删除学生: {student}")
                confirm = input("确认删除吗？(y/n): ").strip().lower()

                if confirm == 'y':
                    self.students.pop(i)
                    print("学生删除成功！")
                else:
                    print("取消删除操作。")
                return

        print(f"未找到学号为 {id} 的学生！")

    def show_statistics(self):
        print("\n----- 成绩统计 -----")
        if not self.students:
            print("没有学生信息！")
            return

        java_total = math_total = english_total = 0
        java_max = math_max = english_max = 0
        java_min = math_min = english_min = 100

        for student in self.students:
            # Java成绩统计
            java_total += student.java_score
            java_max = max(java_max, student.java_score)
            java_min = min(java_min, student.java_score)

            # 数学成绩统计
            math_total += student.math_score
            math_max = max(math_max, student.math_score)
            math_min = min(math_min, student.math_score)

            # 英语成绩统计
            english_total += student.english_score
            english_max = max(english_max, student.english_score)
            english_min = min(english_min, student.english_score)

        count = len(self.students)
        print(f"学生总数: {count}")

        print("\nJava成绩统计:")
        print(f"平均分: {java_total / count:.2f}, 最高分: {java_max:.2f}, 最低分: {java_min:.2f}")

        print("\n数学成绩统计:")
        print(f"平均分: {math_total / count:.2f}, 最高分: {math_max:.2f}, 最低分: {math_min:.2f}")

        print("\n英语成绩统计:")
        print(f"平均分: {english_total / count:.2f}, 最高分: {english_max:.2f}, 最低分: {english_min:.2f}")


def main():
    manager = ScoreManager()
    manager.show_menu()


if __name__ == "__main__":
    main()